package graduation.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import graduation.dto.MeetingDto;
import graduation.entity.Meeting;
import graduation.entity.User;
import graduation.service.MeetingService;
import graduation.service.UserService;
import graduation.utils.ResultVOUtil;
import graduation.vo.GetAllMeetingVO;
import graduation.vo.MeetingVO;
import graduation.vo.ResultVO;

@Controller
@RequestMapping("/meeting")
public class MeetingController {
	@Autowired
	private MeetingService meetingServiceImpl;
	@Autowired
	private UserService userServiceImpl;
	/**
	 * 添加
	 * @param title
	 * @param content
	 * @param name
	 * @param members
	 * @param time
	 * @param location
	 * @return
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public ResultVO add(@RequestBody MeetingVO meetingVO){
		//把字符数组变成字符串
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < meetingVO.getMembers().length; i++){
		 sb. append(meetingVO.getMembers()[i] + ",");
		}
		//得到字符串
		String menber = sb.toString();
		//时间格式2018/08/26 18:00
		//添加到对象中
		Meeting meeting = new Meeting();
		meeting.setTitle(meetingVO.getTitle());
		meeting.setContent(meetingVO.getContent());
		meeting.setMembers(menber);
		meeting.setName(meetingVO.getName());
		meeting.setLocation(meetingVO.getLocation());
		meeting.setTime(meetingVO.getTime());
		meeting.setUserId(meetingVO.getUserId());
		//保存到数据库
		meetingServiceImpl.add(meeting);
		return ResultVOUtil.success("添加成功",meeting);
	}
	
	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="deleteById",method=RequestMethod.POST)
	@ResponseBody
	public ResultVO deleteById(@RequestBody  Map<String,Integer> map){
		//通过id查询数据库
		int id = map.get("id");
		System.out.println(id);
		meetingServiceImpl.deleteById(id);
		return ResultVOUtil.success("删除成功");
		
	}
	/**
	 * 
	 * 获取全部的会议记录
	 * @return
	 */
	@RequestMapping(value="getAllMeeting",method=RequestMethod.GET)
	@ResponseBody
	public ResultVO getAllMeeting(){
		//通过id查询数据库
		List<Meeting> meetingList = meetingServiceImpl.getAllMeeting();
//		使用 Java8 的Stream api ：
//		首先调用stream()方法，将其转换为流
//		在流上调用map方法将其转换为另一个对象流
//		调用collect方法，将对象流收集起来转化为集合
		List<MeetingDto> meetingDtoList = meetingList.stream()
                .map(e -> new MeetingDto(e.getId(),e.getTitle(),e.getContent(),e.getName(),e.getMembers(),e.getTime(),e.getLocation(),e.getUserId(),new ArrayList()))
                .collect(Collectors.toList());
		System.out.println("meetingDtoList"+meetingDtoList);
		//用户名称
		List users = new ArrayList();
		for (MeetingDto meetingDto : meetingDtoList) {
			String member = meetingDto.getMembers();
			//分割字符串，变成字符数组
			String[] members = member.split(",");
			//遍历字符数组
			for(int i = 0; i < members.length; i++){
				//根据id查询是否有对应的用户信息
				System.out.println("用户id"+members[i]);
				System.out.println();
				User user =  userServiceImpl.findUserById(members[i]);
				System.out.println("user"+user);
				if(user != null){
					meetingDto.getUserList().add(user.getName());
				}
			}
		}
		System.out.println("遍历后的返回数据"+meetingDtoList);
		return ResultVOUtil.success(meetingDtoList);
//		for(Meeting meeting : meetingList){
//			String member = meeting.getMembers();
//			String[] members = member.split(",");
//			for(int i = 0; i < members.length; i++){
//				User user = userServiceImpl.findUserById(members[i]);
//				if(user != null){
//					
//					users.add(user.getName());
//				}
//			}
//		}
//		GetAllMeetingVO getAllMeetingVO = new GetAllMeetingVO();
//		getAllMeetingVO.setUserdata(users);
//		getAllMeetingVO.setMeetingData(meetingList);
//		return ResultVOUtil.success(getAllMeetingVO);
	}
	/**
	 * 
	 * 通过判断members是否有我的id，获取有我份的会议记录
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="getMyMeeting",method=RequestMethod.GET)
	@ResponseBody
	public ResultVO getMyMeeting(@RequestParam("userId") String userId){
		List myMeeting = new ArrayList();
		//查询member中是否有相同的用户id
		//查询用户中的全部members
		List<Meeting> list = meetingServiceImpl.getAllMeeting();
		for (Meeting meeting : list) {
			String member = meeting.getMembers();
			String [] members = member.split(",");
			for(int i = 0; i < members.length; i++){
				 if(members[i].equals(userId)){
					 //有相同的userId加入到myMeeting中
					 myMeeting.add(meeting);
				 }
			}
		}
		if(myMeeting.size() < 1){
			return ResultVOUtil.fail("该用户没有相应的申请");
		}
		return  ResultVOUtil.success(myMeeting);
		
	}
	/**
	 * 根据会议时间判断
	 * @return
	 */
	@RequestMapping(value="getFutureMeeting",method=RequestMethod.GET)
	@ResponseBody
	public ResultVO getFutureMeeting(){
		//获取当前时间
		Date nowDate = new Date();
		//获取全部的会议表
		List<Meeting> list = meetingServiceImpl.getAllMeeting();
		//
		List showList = new ArrayList();
		for(Meeting meeting : list){
			String time = meeting.getTime();
			//相等则返回0,nowDate大返回1,否则返回-1;
			////时间格式2018/08/26 18:00
			SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm");  
			Date date;
			try {
				date = format.parse(time);
				int res = nowDate.compareTo(date);
				//会议时间大于现在时间，就显示
				if(res==-1){
					showList.add(meeting);
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		
		return ResultVOUtil.success("成功",showList);
		
	}
}
